package com.tech.common

import com.tech.config.ApplicationConfig

import java.text.SimpleDateFormat
import java.util.{Calendar, Date, TimeZone}

object DateUtil {

  val dayFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
  dayFormat.setTimeZone(TimeZone.getTimeZone(ApplicationConfig.TIMEZONE))

  //获取昨天的日期
  def getYesterday: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, -1)
    dayFormat.format(cal.getTime)
  }

  //获取今天日期
  def getToday: String = {
    val now: Date = new Date()
    dayFormat.format(now)
  }

  //获取上周开始日期
  def getLastWeekStart: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
    cal.add(Calendar.WEEK_OF_YEAR, -1)
    dayFormat.format(cal.getTime)
  }

  //获取上周结束时间
  def getLastWeekEnd: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
    dayFormat.format(cal.getTime)
  }

  //获取本周开始日期
  def getThisWeekStart: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
    dayFormat.format(cal.getTime)
  }

  //获取本周结束时间
  def getThisWeekEnd: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
    cal.add(Calendar.WEEK_OF_YEAR, 1)
    dayFormat.format(cal.getTime)
  }

  //上月的第一天
  def getLastMonthStart: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DATE, 1)
    cal.add(Calendar.MONTH, -1)
    dayFormat.format(cal.getTime)
  }

  //上月的最后一天
  def getLastMonthEnd: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DATE, 1)
    cal.add(Calendar.MONTH, -1)
    cal.roll(Calendar.DATE, -1)
    dayFormat.format(cal.getTime)
  }

  //本月的第一天
  def getThisMonthStart: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DATE, 1)
    dayFormat.format(cal.getTime)
  }

  //本月的最后一天
  def getThisMonthEnd: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.DATE, 1)
    cal.roll(Calendar.DATE, -1)
    dayFormat.format(cal.getTime)
  }

  //去年的第一天
  def getLastYearStart: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.MONTH, 1)
    cal.add(Calendar.YEAR, -1)
    cal.add(Calendar.MONTH, -1)
    cal.set(Calendar.DATE, 1)
    dayFormat.format(cal.getTime)
  }

  //去年的最后一天
  def getLastYearEnd: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.MONTH, 1)
    cal.add(Calendar.MONTH, -1)
    cal.set(Calendar.DATE, 1)
    cal.add(Calendar.DATE, -1)
    dayFormat.format(cal.getTime)
  }

  //本年的第一天
  def getThisYearStart: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.set(Calendar.MONTH, 1)
    cal.add(Calendar.MONTH, -1)
    cal.set(Calendar.DATE, 1)
    dayFormat.format(cal.getTime)
  }

  //本年的最后一天
  def getThisYearEnd: String = {
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.YEAR, 1)
    cal.set(Calendar.MONTH, 1)
    cal.add(Calendar.MONTH, -1)
    cal.set(Calendar.DATE, 1)
    cal.add(Calendar.DATE, -1)
    dayFormat.format(cal.getTime)
  }

}
